var express = require('express');
var router = express.Router();
const { MemberModel, RecallModel, ExchangeModel } = require('../model/cxy_model');
const { log } = require('console');

// 成员管理获取数据
router.get('/getMember', async (req, res) => {
	let pageIndex = req.query.page || 1
	let pageSize = req.query.pageSize || 3
	let searchTxt = req.query.kw
	console.log(pageIndex);
	let reg = new RegExp(searchTxt, 'i')
	console.log(reg);
	let list;
	let total = 0
	let totalpage = 0
	let all = await MemberModel.find().populate("sect")
	if (searchTxt) {
		list = await MemberModel.find({ cname: reg }).populate("sect").skip((pageIndex - 1) * pageSize).limit(pageSize)
		total = await MemberModel.find({ cname: reg }).count();
		totalpage = Math.ceil(total / pageSize);
	} else {
		list = await MemberModel.find().populate("sect").skip((pageIndex - 1) * pageSize).limit(pageSize)
		total = await MemberModel.count();
		totalpage = Math.ceil(total / pageSize);
	}
	res.send({
		code: 200,
		data: list,
		total: total,
		all,
		totalpage
	})
});

// 成员管理修改
router.post("/editMember", async (req, res) => {
	// let id = req.params.id
	let body = req.body.gai
	let _id = body._id
	let cname = body.cname
	let id = body.id
	let sex = body.sex
	let shou = body.shou
	let rtime = body.rtime
	let zhi = body.zhi
	// console.log(body);
	await MemberModel.updateOne({ _id }, { id, cname, sex, shou, rtime, zhi });
	// let one = await MemberModel.find({ _id: body.id })
	res.send({
		code: 200,
		// one
	})
})

// 成员管理添加
router.post("/Memberadd", async (req, res) => {
	// console.log(req.body);
	let body = req.body.objs;
	let data = await MemberModel.create(body);
	res.send({
		code: 200,
		msg: '添加成功',
		data
	})
});


// 回收订单管理获取数据
router.get('/getRecall', async (req, res) => {
	let pageIndex = req.query.page || 1
	let pageSize = req.query.pageSize || 3
	let searchTxt = req.query.kw
	console.log(pageIndex);
	let reg = new RegExp(searchTxt, 'i')
	console.log(reg);
	let list;
	let total = 0
	let totalpage = 0
	let all = await RecallModel.find().populate("act")
	if (searchTxt) {
		list = await RecallModel.find({ iphone: reg }).populate("act").skip((pageIndex - 1) * pageSize).limit(pageSize)
		total = await RecallModel.find({ iphone: reg }).count();
		totalpage = Math.ceil(total / pageSize);
	} else {
		list = await RecallModel.find().populate("act").skip((pageIndex - 1) * pageSize).limit(pageSize)
		total = await RecallModel.count();
		totalpage = Math.ceil(total / pageSize);
	}
	res.send({
		code: 200,
		data: list,
		total: total,
		all,
		totalpage
	})
});

// 收货员添加
router.post("/Recalladd", async (req, res) => {
	// console.log(req.body);
	let body = req.body;
	let data = await RecallModel.create(body);
	res.send({
		code: 200,
		msg: '添加成功',
		data
	})
});

// 兑换订单管理获取数据
router.get('/getExchange', async (req, res) => {
	let pageIndex = req.query.page || 1
	let pageSize = req.query.pageSize || 3
	let searchTxt = req.query.kw
	console.log(pageIndex);
	let reg = new RegExp(searchTxt, 'i')
	console.log(reg);
	let list;
	let total = 0
	let totalpage = 0
	let all = await ExchangeModel.find().populate("yui")
	if (searchTxt) {
		list = await ExchangeModel.find({ name: reg }).populate("yui").skip((pageIndex - 1) * pageSize).limit(pageSize)
		total = await ExchangeModel.find({ name: reg }).count();
		totalpage = Math.ceil(total / pageSize);
	} else {
		list = await ExchangeModel.find().populate("yui").skip((pageIndex - 1) * pageSize).limit(pageSize)
		total = await ExchangeModel.count();
		totalpage = Math.ceil(total / pageSize);
	}
	res.send({
		code: 200,
		data: list,
		total: total,
		all,
		totalpage
	})
});
module.exports = router;